<?php

use think\migration\Migrator;
use think\migration\db\Column;

class SyncCouponOrderDataTemplateId extends Migrator
{

    public function change()
    {
        \app\model\CouponOrder::query()
            ->whereNotNull('cnid')
            ->select()->each(function (\app\model\CouponOrder $couponOrder) {
                $couponOrder->template_id = !empty($couponOrder->agent_channel) ? $couponOrder->agent_channel->template_id : 0;
                $couponOrder->save();
            });

    }
}
